import {message} from 'antd';

import {
    query as queryUsers, remove, check, checkUser, addUser, status, queryCurrent, backUser,
    removeUserCheck,changeShow, queryCheckInfo, editUser, queryChangeCheckInfo, queryChangeServiceCheck, queryNewServiceCheck,
} from '../services/user';

export default {
    namespace: 'user',

    state: {
        data: {list: [], pagination: {size: 10, current: 1, total: 0}},
        checks: {list: [], pagination: {size: 10, current: 1, total: 0}},
        loading: false,

        currentUser: {},
        created_at: [],
        changes: {list: [], pagination: {size: 10, current: 1, total: 0}},
        checkChanges: {pagination: {current: 1, size: 10, total: 0}, list: []},
        checkInfo: {ing: 0, checked: 0, uncheck: 0},
        confirmLoading: false,
        modalVisible: false,
    },

    effects: {
        * fetch({payload}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const response = yield call(queryUsers, payload);
            yield put({
                type: 'save',
                payload: response,
            });
            yield put({
                type: 'changeLoading',
                payload: false,
            });
        },
        * check_count({payload}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const response = yield call(queryCheckInfo, payload);
            yield put({
                type: 'saveCheckInfo',
                payload: response.data,
            });
            yield put({
                type: 'changeLoading',
                payload: false,
            });
        },
        * fetchChanges({payload, callback}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const result = yield call(queryChangeCheckInfo, payload);
            yield put({
                type: 'saveChanges',
                payload: result,
            });
            if (callback) {
                callback();
            }
            yield put({
                type: 'changeLoading',
                payload: false,
            });
        },
        * fetchCheckChanges({payload, callback}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const result = yield call(queryChangeCheckInfo, payload);
            yield put({
                type: 'saveCheckChanges',
                payload: result,
            });
            if (callback) {
                callback();
            }
            yield put({
                type: 'changeLoading',
                payload: false,
            });
        },
        * queryNewChange({payload, callback}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const result = yield call(queryNewServiceCheck, payload);
            yield put({
                type: 'saveNewChange',
                payload: result,
            });
            if (callback) {
                callback();
            }
            yield put({
                type: 'changeLoading',
                payload: false,
            });
        },

        * changeServiceCheck({payload, callback}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const result = yield call(queryChangeServiceCheck, payload);
            if (result.status) {
                message.success('修改成功');
                if (callback) {
                    callback();
                }
            } else {
                message.error('修改失败');
            }

            yield put({
                type: 'changeLoading',
                payload: false,
            });
        },
        * status({payload, callback}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const result = yield call(status, payload);
            if (result.status) {
                message.success('修改成功');
                yield put({
                    type: 'fetch',
                });
                if (callback) {
                    callback();
                }
            } else {
                message.error('修改失败');
            }
        },
        * indexChange({payload,callback},{call,put}){
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const result = yield call(changeShow, payload);
            if (result.status) {
                message.success('修改成功');
                if (callback) {
                    callback();
                }
            } else {
                message.error('修改失败');
            }
        },
        * add({payload, callback}, {call, put}) {
            yield put({
                type: 'changeCreateLoading',
                payload: true,
            });
            const result = yield call(addUser, payload);
            if (callback) {
                callback(result);
            }
            yield put({
                type: 'changeCreateLoading',
                payload: false,
            });
        },
        * check({payload, callback}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const response = yield call(check, payload);
            yield put({
                type: 'saveChecks',
                payload: response,
            });
            yield put({
                type: 'changeLoading',
                payload: false,
            });
            if (callback) {
                callback();
            }
        },

        * edit({payload, callback}, {call, put}) {
            yield put({
                type: 'changeCreateLoading',
                payload: true,
            });
            const response = yield call(editUser, payload);
            if (response.status) {
                message.success('编辑成功');
            } else {
                message.error('编辑失败');
            }
            if (response && callback) {
                callback(response);
            }
            yield put({
                type: 'changeCreateLoading',
                payload: false,
            });
        },

        * accredit({payload, callback}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const response = yield call(checkUser, payload);
            if (response.status) {
                message.success('审核成功');
            } else {
                message.error('审核失败');
            }
            if (response && callback) {
                callback(response);
            }
            yield put({
                type: 'changeLoading',
                payload: false,
            });
        },
        * back({payload, callback}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const response = yield call(backUser, payload);
            if (response.status) {
                message.success(response.message);
            } else {
                message.error(response.message);
            }
            if (response && callback) {
                callback(response);
            }
            yield put({
                type: 'changeLoading',
                payload: false,
            });
        },
        * checkRemove({payload, callback}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const response = yield call(removeUserCheck, payload);
            if (response.status) {
                message.success(response.message);
            } else {
                message.error(response.message);
            }
            if (response && callback) {
                callback(response);
            }
            yield put({
                type: 'changeLoading',
                payload: false,
            });
        },
        * remove({payload}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const result = yield call(remove, payload);
            if (result.status) {
                message.success('删除成功');
                yield put({
                    type: 'fetch',
                });
            } else {
                message.error('删除失败');
            }
        },
        * fetchCurrent(_, {call, put}) {
            const response = yield call(queryCurrent);
            yield put({
                type: 'saveCurrentUser',
                payload: response.data,
            });
        },
    },

    reducers: {
        save(state, action) {
            return {
                ...state,
                data: {list: action.payload.data, pagination: action.payload.page},
            };
        },
        changeCreateLoading(state, action) {
            return {
                ...state,
                confirmLoading: action.payload,
            };
        },
        changeLoading(state, action) {
            return {
                ...state,
                loading: action.payload,
            };
        },
        saveCheckInfo(state, {payload}) {
            return {
                ...state,
                checkInfo: payload
            };
        },
        saveNewChange(state, {payload}) {
            return {
                ...state,
                newChange: payload.data
            }
        },
        saveChecks(state, action) {
            return {
                ...state,
                checks: {list: action.payload.data, pagination: action.payload.page},
            };
        },
        saveCheckChanges(state,{payload}){
            return {
                ...state,
                checkChanges: {list: payload.data, pagination: payload.page},
            };
        },
        saveChanges(state, action) {
            return {
                ...state,
                changes: {list: action.payload.data, pagination: action.payload.page},
            };
        },
        changeVisible(state, action) {
            return {
                ...state,
                modalVisible: action.payload,
            };
        },
        saveCurrentUser(state, action) {
            return {
                ...state,
                currentUser: action.payload,
            };
        },
        changeNotifyCount(state, action) {
            return {
                ...state,
                currentUser: {
                    ...state.currentUser,
                    notifyCount: action.payload,
                },
            };
        },
    },
};
